import { ITreeNode } from './interface';

// columns转换
export function columnsToTableColumns(columns: any[]) {
  return columns
    .filter((r) => r.isDisplay)
    .map((r) => ({
      sortable: false,
      dataIndex: r.fieldName,
      title: r.fieldAlias || r.fieldName,
      flex: 1,
      format: r.format,
      width: r.fieldWidth || 100
    }));
}

// queryItems转换
export function queryItemsToFormItems(queryItems: any[]) {
  return queryItems.map((r) => {
    const item: any = { label: r.fieldAlias || r.fieldName, name: r.fieldName };
    r.helpId && (item.helpId = r.helpId);
    r.displayType && (item.xtype = r.displayType);
    r.enumValue &&
      (item.data = r.enumValue.split(';').map((item) => {
        const vl = item.split('|');
        return { value: vl[0], label: vl[1] };
      }));
    return item;
  });
}
/**
 * 根据filters过滤符合条件的index集合
 * @param arr
 * @param filters
 * @returns
 */
export function findFilterIndexes(arr, func) {
  const result: number[] = [];
  arr.forEach((it, index) => {
    if (func(it)) {
      result.push(index);
    }
  });
  return result;
}

/**
 * 递归找到当前节点&子节点keys
 *  @param treeList
 *  @returns string[]
 */
export function loopTreeChildren(treeList: ITreeNode[]): string[] {
  let result: string[] = [];
  treeList.forEach((it: ITreeNode) => {
    result.push(it.key);
    if (it.children?.length) {
      result = result.concat(loopTreeChildren(it.children));
    }
  });
  return result;
}
